package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysf.library_boot.dto.BookCommentDto;
import com.zysf.library_boot.dto.MyCommentDto;
import com.zysf.library_boot.dto.ReaderLendDto;
import com.zysf.library_boot.entity.BookComment;
import com.zysf.library_boot.service.BookCommentService;
import com.zysf.library_boot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bookComment")
public class BookCommentController {

    @Autowired
    BookCommentService bookCommentService;
    @RequestMapping("/addBookComment")
    public Result addBookComment(@RequestBody BookComment bookComment){
        boolean b = bookCommentService.save(bookComment);
        return Result.ok(b);
    }
    @RequestMapping("/showBookComment")
    public Result showBookComment(Integer bookid,Integer page,Integer size){
        List<BookCommentDto> bookCommentDtos  = bookCommentService.showBookComment(bookid,page,size);
        return Result.ok(bookCommentDtos);
    }

    @RequestMapping("/countCommentByBookid")
    public Result countCommentByBookid(Integer bookid){
        QueryWrapper<BookComment> qw = new QueryWrapper<>();
        qw.eq("bookid",bookid);
        int totalPage = (int) bookCommentService.count(qw);
        return  Result.ok(totalPage);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean q = bookCommentService.removeById(id);
        return Result.ok(q);
    }

    @RequestMapping("/getMyComment")
    public Result getMyComment(@RequestParam Integer id, @RequestParam int currentPage, @RequestParam int pageSize) {
        Page<MyCommentDto> page = new Page<>(currentPage, pageSize);
        IPage<MyCommentDto> myCommentDtoPage = bookCommentService.getReaderMyComment(id, page);

        Map<String, Object> result = new HashMap<>();
        result.put("records", myCommentDtoPage.getRecords());
        result.put("total", myCommentDtoPage.getTotal());

        return Result.ok(result);
    }
}
